﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Maticsoft.DAL
{
    public static class SqlHelper
    {
        public static string sqlConn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;//得到连接字符串


        /// <summary>
        /// 存储过程
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="ty"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReaderPro(string sql,CommandType ty, params SqlParameter[] para)
        {
            SqlConnection conn = new SqlConnection(sqlConn);
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                if (para != null)
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddRange(para);

                }
                try
                {
                    conn.Open();
                    return cmd.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch
                {
                    conn.Close();
                    conn.Dispose();
                    throw;
                }
            }


        }


        public static int ExecuteNonquery(string sql, CommandType cm, params SqlParameter[] sqlParameter)
        {
            using (SqlConnection conn = new SqlConnection(sqlConn))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    if (sqlParameter != null)
                    {
                        cmd.Parameters.AddRange(sqlParameter);
                        cmd.CommandType = cm;
                    }
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                   
                }
            }
        }


        /// <summary>
        /// 执行增、删、改
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public static int ExecuteNonquery(string sql, params SqlParameter[] para)
        {
            using (SqlConnection conn = new SqlConnection(sqlConn))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    if (para != null)
                    {
                        cmd.Parameters.AddRange(para);
                    }
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }


        /// <summary>
        /// 返回结果集里面第一行第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, params SqlParameter[] para)
        {
            using (SqlConnection conn = new SqlConnection(sqlConn))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    if (para != null)
                    {
                        cmd.Parameters.AddRange(para);
                    }
                    conn.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }


        /// <summary>
        /// 返回集合
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReader(string sql,  params SqlParameter[] para)
        {
            SqlConnection conn = new SqlConnection(sqlConn);
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                if (para != null)
                {
                    cmd.Parameters.AddRange(para);
                    
                }
                try
                {
                    conn.Open();
                    return cmd.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch
                {
                    conn.Close();
                    conn.Dispose();
                    throw;
                }
            }


        }



        /// <summary>
        /// 利用SqlDataAdapter返回一个DataTable
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public static DataTable MyDataTable(string sql, params SqlParameter[] para)
        {
            using (SqlDataAdapter da=new SqlDataAdapter(sql,sqlConn))
            {
                if (para!=null)
                {
                    da.SelectCommand.Parameters.AddRange(para);
                }
                DataTable ds = new DataTable();
                da.Fill(ds);
                return ds;
            }
             
        }



        /// <summary>
        /// 执行分页的存储过程
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="c"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public static DataTable MyDataTableFenYe(string sql,CommandType c, params SqlParameter[] para)
        {
            DataTable ta = new DataTable();

            using (SqlConnection conn=new SqlConnection(sqlConn))
            {
                using (SqlCommand cmd=new SqlCommand(sql,conn))
                {
                    if (para!=null)
                    {
                        conn.Open();
                        cmd.Parameters.AddRange(para);
                        cmd.CommandType = c;
                    }
                    using (SqlDataAdapter da=new SqlDataAdapter(cmd))
                    {
                        da.Fill(ta);
                    }
                }
            }
            return ta;

        }



    }
}
